package com.rentong.wx.cust.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.rentong.wx.cust.dao.CustServiceOrderDao;
import com.rentong.wx.cust.dao.CustServiceOrderShouhouDao;
import com.rentong.wx.cust.domain.CustServiceOrderEntity;
import com.rentong.wx.cust.domain.CustServiceOrderShouhouEntity;
import com.rentong.wx.cust.service.CustServiceOrderShouhouService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import scala.Int;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class CustServiceOrderShouhouServiceImpl implements CustServiceOrderShouhouService {

    @Resource
    private CustServiceOrderShouhouDao custServiceOrderShouhouDao;

    @Autowired
    private CustServiceOrderDao custServiceOrderDao;

    @Override
    public void submitShouhou(CustServiceOrderShouhouEntity shouhouEntity, Integer type) {
        //type 1：申请售后 2：取消售后
        if (type == 1) {
            CustServiceOrderShouhouEntity shouhouByOrderId = custServiceOrderShouhouDao.getShouhouByOrderId(shouhouEntity.getServiceOrderId());
            if (ObjectUtil.isNotEmpty(shouhouByOrderId)) {
                shouhouEntity.setServiceOrderShouhouId(shouhouByOrderId.getServiceOrderShouhouId());
                shouhouEntity.setShouhouState(1);
                shouhouEntity.setUpdateTime(new Date());
                custServiceOrderShouhouDao.updateByPrimaryKey(shouhouEntity);
            } else {
                //插入cust_service_order_state
                shouhouEntity.setShouhouState(1);
                shouhouEntity.setUpdateTime(new Date());
                shouhouEntity.setAddTime(new Date());
                custServiceOrderShouhouDao.insert(shouhouEntity);
            }


            //更新cust_service_order的状态
            CustServiceOrderEntity custServiceOrderEntity = new CustServiceOrderEntity();
            CustServiceOrderEntity custServiceOrder = custServiceOrderDao.selectByPrimaryKey(shouhouEntity.getServiceOrderId());
            custServiceOrderEntity.setServiceOrderId(shouhouEntity.getServiceOrderId());
            custServiceOrderEntity.setOrderStateInitial(custServiceOrder.getOrderState());
            if (custServiceOrder.getOrderState() == 9) {
                custServiceOrderEntity.setOrderStateInitial(custServiceOrder.getOrderStateInitial());
            }
            custServiceOrderEntity.setOrderState(8);
            custServiceOrderEntity.setUpdateTime(new Date());

            //继续下，这笔订单是否申请过售后，前端需要用来做判断用的
            custServiceOrderEntity.setIsShouhou(1);
            custServiceOrderDao.updateByPrimaryKeySelective(custServiceOrderEntity);
        } else if (type == 2) {
            custServiceOrderShouhouDao.updateStat(shouhouEntity.getServiceOrderId());

            //更新cust_service_order的状态
            CustServiceOrderEntity custServiceOrderEntity = new CustServiceOrderEntity();
            CustServiceOrderEntity custServiceOrder = custServiceOrderDao.selectByPrimaryKey(shouhouEntity.getServiceOrderId());
            custServiceOrderEntity.setServiceOrderId(shouhouEntity.getServiceOrderId());
            custServiceOrderEntity.setOrderState(custServiceOrder.getOrderStateInitial());
            custServiceOrderEntity.setUpdateTime(new Date());

            //继续下，这笔订单是否申请过售后，前端需要用来做判断用的
            custServiceOrderEntity.setIsShouhou(2);
            custServiceOrderDao.updateByPrimaryKeySelective(custServiceOrderEntity);

        }
    }

    @Override
    public CustServiceOrderShouhouEntity getShouhou(Long serviceOrderId) {
        return custServiceOrderShouhouDao.getShouhouByOrderId(serviceOrderId);
    }
}